﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using LongQuanWeb.App_Code;
using Maticsoft.Common;
using DotNet.Utilities;

namespace FeiHongWeb.Admin
{
    public partial class ProductAddUpdate : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((this.Session["username"] == null) || (this.Session["username"].ToString() == string.Empty))
            {
                base.Response.Write("<font color=#ff0000 style='FONT-SIZE: 12px'>对不起,您没足够权限访问此页!!</font><br>");
                base.Response.Write("<a href='Login.aspx' target=_top style='FONT-SIZE: 12px'>重新登陆</a><br>");
                base.Response.End();
            }
            if (!IsPostBack)
            {
                base.Response.Write("<script>parent.window.document.getElementById( 'lbl_title').innerHTML='人才信息'; </script>");
                string type = Request.QueryString["type"].ToString().Trim();
                //人才类别
                if (type == "1")
                {
                    ddl_type.Text = ddl_type.Items[0].ToString();
                }

                else if (type == "2")
                {
                    string id = Request.QueryString["id"].ToString().Trim();
                    string sql = "select * from [Volunteer] where Id=" + id + "";
                    DataSet ds = DbHelperSQL.Query(sql);
                    if (ds.Tables[0] != null && ds.Tables[0].Rows.Count != 0)
                    {
                        this.ddl_type.SelectedValue = ds.Tables[0].Rows[0]["VolunteerType"].ToString();
                        elm6.Value = ds.Tables[0].Rows[0]["Description"].ToString();
                        this.tbx_Name.Text = ds.Tables[0].Rows[0]["VolunteerName"].ToString();
                    }
                }
                btn_return.CommandArgument = type;
            }
        }

        protected void btn_save_Click(object sender, EventArgs e)
        {
            //int count = 0;
            string type = btn_return.CommandArgument.ToString();
            try
            {
                string picName = "";
                if (tbx_Name.Text.Trim() == "")
                {
                    MessageBox.Show(this, "志愿者姓名不能为空");
                    return;
                }
                string Name = tbx_Name.Text.Trim();
                string rate = ddl_Rate.SelectedValue.ToString();
                string description = elm6.Value;
                if (type == "1")
                {
                    if (upload.HasFile == false)
                    {
                        base.Response.Write("<script>alert('请选择志愿者图片！');</script>");
                        return;
                    }
                    string fullname = upload.PostedFile.FileName;
                    //string sqlStr = "SELECT * FROM Volunteer";
                    //count = DbHelperSQL.ExecuteSql(sqlStr)+1;
                    FileInfo fi = new FileInfo(fullname);
                    string pictype = fi.Extension;
                    if (".jpg" == pictype || ".JPG" == pictype)
                    {
                        //定义保存图片的路径
                        string savepath = Server.MapPath("~/upload/Volunteer");
                        //保存上传的图片
                        //upload.PostedFile.SaveAs();
                        string frontname = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                        picName = frontname+"_s.jpg";
                        ImageUpload imageclass = new ImageUpload();
                        imageclass.IsCreateImg = true;
                        imageclass.SavePath = "~/upload/Volunteer/";
                        imageclass.FU_FUFormFile = upload;
                        System.Web.UI.HtmlControls.HtmlInputFile f1 =new System.Web.UI.HtmlControls.HtmlInputFile();
                        imageclass.OutFileName =frontname+".jpg" ;
                        imageclass.InFileName = frontname + ".jpg";
                        imageclass.SaveType = 1;
                        imageclass.Upload();
                    }
                    else
                    {
                        Response.Write("<script>alert('请选择jpg格式的图片！')</script>");
                        return;
                    }

                    string strSql = "insert into [Volunteer](VolunteerName,PhotoPath,Description,Title,VolunteerType,Rate) values('" + Name + "','" + picName + "','" + description + "','" + "null" + "','" + this.ddl_type.SelectedValue.ToString()+"','"+rate + "') ";
                    if (DbHelperSQL.ExecuteSql(strSql) > 0)
                    {
                        base.Response.Write("<script>alert('添加成功');window.location='ProductAddUpdate.aspx?type=1'</script>");
                    }
                    else
                    {
                        base.Response.Write("<script>alert('添加失败');</script>");
                    }
                }
                else if (type == "2")
                {
                    string id = Request.QueryString["id"].ToString().Trim();
                    string strFile = "select * from [Volunteer] where Id=" + id + "";
                    DataTable dtfile = DbHelperSQL.Query(strFile).Tables[0] ;
                    if (dtfile != null && dtfile.Rows.Count != 0)
                    {
                        picName = dtfile.Rows[0]["PhotoPath"].ToString().Trim();
                    }


                    if (upload.HasFile)
                    {
                        string fullname = upload.PostedFile.FileName;
                        FileInfo fi = new FileInfo(fullname);
                        string pictype = fi.Extension;
                        if (".jpg" == pictype || ".JPG" == pictype)
                        {
                            //定义保存图片的路径
                            string savepath = Server.MapPath("~/upload/Volunteer");
                            //保存上传的图片
                            //upload.PostedFile.SaveAs();
                            string frontname = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                            picName = frontname + "_s.jpg";
                            ImageUpload imageclass = new ImageUpload();
                            imageclass.IsCreateImg = true;
                            imageclass.SavePath = "~/upload/Volunteer/";
                            imageclass.FU_FUFormFile = upload;
                            System.Web.UI.HtmlControls.HtmlInputFile f1 = new System.Web.UI.HtmlControls.HtmlInputFile();
                            imageclass.OutFileName = frontname + ".jpg";
                            imageclass.InFileName = frontname + ".jpg";
                            imageclass.SaveType = 1;
                            imageclass.Upload();
                        }
                        else
                        {
                            Response.Write("<script>alert('请选择jpg格式的图片！')</script>");
                            return;
                        }
                    }

                    string sql = "Update [Volunteer] set Description='" + this.elm6.Value + "',PhotoPath='" + picName + "',title='" + "null" + "',VolunteerName='" + Name + "',VolunteerType='" + ddl_type.SelectedValue.ToString() + "',Rate='" + rate + "' where ID=" + id + " ";
                    if (DbHelperSQL.ExecuteSql(sql) > 0)
                    {
                        base.Response.Write("<script>alert('修改成功');window.location='Product.aspx'</script>");
                    }
                    else
                    {
                        base.Response.Write("<script>alert('修改失败');</script>");
                    }
                }
            }
            catch (Exception er)
            {
                Response.Write(" <script defer>alert('" + er.Message + "') </script>");
            }
        }

        protected void btn_return_Click(object sender, EventArgs e)
        {
            string type = btn_return.CommandArgument.ToString();
            if (type == "1")
            {
                Response.Redirect("ProductAddUpdate.aspx?type=1");
            }
            else if (type == "2")
            {
                Response.Redirect("Product.aspx");
            }
        }
    }
}